<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\DataContentChip as DataContentChipModel;
use think\facade\Db;

class DataContentChip extends AdminBase
{
    public function index()
    {
        $param = $this->request->param();
        $model = new DataContentChipModel();
        if(!empty($param['position1'])) {
            $model = $model->where('position1',$param['position1']);
            if($param['position1'] == 3 && !empty($param['position2'])){
                $model = $model->where('position2',$param['position2']);
            }
        }
        if(!empty($param['kw'])){
            $model = $model->where('title','like','%'.$param['kw'].'%');
        }
        $list = $model->order('id desc')->paginate(['query' => $param]);
        $count = $list->total();
        $position1List = Db::name('data_content_chip_position')->where('pid','=',0)->select();
        $position2List = Db::name('data_content_chip_position')->where('pid','>',0)->order('sort asc')->select();
        return view('',['list'=>$list,'param'=>$param,'count'=>$count,'position1List'=>$position1List,'position2List'=>$position2List]);
    }

    public function add()
    {
        if( $this->request->isPost() ) {
            $param = $this->request->param();
            $result = DataContentChipModel::create($param);
            if( $result ) {
                xn_add_admin_log('添加单独素材');
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
        $position1List = Db::name('data_content_chip_position')->where('pid','=',0)->select();
        $position2List = Db::name('data_content_chip_position')->where('pid','>',0)->order('sort asc')->select();
        return view('form',['position1List'=>$position1List,'position2List'=>$position2List]);
    }

    public function edit()
    {
        if( $this->request->isPost() ) {
            $param = $this->request->param();
            $result = DataContentChipModel::update($param);
            if( $result ) {
                xn_add_admin_log('修改单独素材');
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
        $id = $this->request->get('id');
        $data = DataContentChipModel::find($id);
        $position1List = Db::name('data_content_chip_position')->where('pid','=',0)->select();
        $position2List = Db::name('data_content_chip_position')->where('pid','>',0)->order('sort asc')->select();
        return view('form',['data'=>$data,'position1List'=>$position1List,'position2List'=>$position2List]);
    }

    public function delete()
    {
        $id = intval($this->request->get('id'));
        !($id>0) && $this->error('参数错误');
        DataContentChipModel::destroy($id);
        xn_add_admin_log('删除单独素材');
        $this->success('删除成功');
    }

    public function deleteAll(){
        $ids = $this->request->get('ids');
        ($ids=='') && $this->error('参数错误');
        $ids = explode(',',$ids);
        DataContentChipModel::destroy($ids);
        xn_add_admin_log('批量删除单独素材');
        $this->success("删除成功");
    }

    public function import(){
        $file = request()->file('file');
        $savename = \think\facade\Filesystem::putFileAs( 'excel', $file, 'chip.xlsx');
        $filePath = \think\facade\Filesystem::path($savename);

        if(!file_exists($filePath)){
            return json(['status'=>'0','msg'=>'文件不存在!']);
        }

        $PHPReader = new \PHPExcel_Reader_Excel2007();
        if(!$PHPReader->canRead($filePath)) {
            $PHPReader = new \PHPExcel_Reader_Excel5();
            if(!$PHPReader->canRead($filePath)) {
                return json(['status'=>'0','msg'=>'No Excel!']);
            }
        }

        $PHPExcel = $PHPReader->load($filePath);
        $currentSheet = $PHPExcel->getSheet(0);
        //$allColumn = $currentSheet->getHighestColumn();
        $allRow = $currentSheet->getHighestRow();

        $limitNum = 10;
        $limitPage = ceil($allRow / $limitNum);
        $count = 0;
        for($i=0;$i<$limitPage;$i++){
            $limitIndex = $i * $limitNum;
            $limitEnd = $limitIndex + $limitNum;
            if($i == 0){
                $limitIndex = 1;
            }
            if($limitEnd >= $allRow){
                $limitEnd = $allRow+1;
            }
            for($currentRow = $limitIndex; $currentRow < $limitEnd; $currentRow++) {
                $data['title'] = $currentSheet->getCell('C'.$currentRow)->getValue();
                $data['position1'] = $currentSheet->getCell('A'.$currentRow)->getValue();
                if(!empty($data['title'])){
                    switch ($data['position1']) {
                        case '条件':
                            $data['position1'] = 1;
                            break;

                        case '优势':
                            $data['position1'] = 2;
                            break;

                        case '流程':
                            $data['position1'] = 3;
                            $pos_name = $currentSheet->getCell('B'.$currentRow)->getValue();
                            $pos_id = Db::name('data_content_chip_position')->field('id')->where([
                                'pid' => 3,
                                'title' => $pos_name
                            ])->find();
                            if($pos_id){
                                $data['position2'] = $pos_id['id'];
                            }
                            break;
                        
                        case '支持':
                            $data['position1'] = 4;
                            $pos_name = $currentSheet->getCell('B'.$currentRow)->getValue();
                            $pos_id = Db::name('data_content_chip_position')->field('id')->where([
                                'pid' => 4,
                                'title' => $pos_name
                            ])->find();
                            if($pos_id){
                                $data['position2'] = $pos_id['id'];
                            }
                            break;
                        
                        default:
                            $data['position1'] = 0;
                            break;
                    }
                    $result = DataContentChipModel::create($data);
                    if($result){
                        $count++;
                    }
                }
            }
        }
        return json(['status'=>'1','msg'=>'成功导入 '.$count.' 条数据']);

    }


}
